import scrapy


class A政府采购合同Spider(scrapy.Spider):
    name = "政府采购合同"
    async def start(self):
        for i in range(0,20):
            headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"}
            yield scrapy.FormRequest(f'http://htgs.ccgp.gov.cn/GS8/contractpublish/getContractByAjax?contractSign=0',headers=headers,formdata={
                "currentPage":f"{i}",
                "codeResult":'967c7283b017a7291fc46cfb913962fc',
                "code":'pageNoChange'
            })


    def parse(self, response):
        datas = response.json()['rows']
        for data in datas:
            url =f'http://htgs.ccgp.gov.cn/GS8/contractpublish/detail/{data['uuid']}'
            print(url)
            yield scrapy.Request(url,callback=self.parse2)
            # break
    def parse2(self, response):
        items = response.xpath('//div[@class="vT_detail_header"]')
        title = items.xpath('./h2/text()').get()
        print(title)
        ds = response.xpath('//div[@class="content_2020"]')
        ps = ds.xpath('./p')
        for p in ps:
            if p.xpath('./strong/text()').get():
                print(p.xpath('./strong/text()').get())
            else:
                print(p.xpath('./text()').get())
        pdf_url = f'http://download.ccgp.gov.cn/oss/download?uuid={response.xpath('//ul//a/@onclick').get().split("'")[1]}'
        print(pdf_url)



